<?php

namespace App\Models\Factory\V0;



use App\Helpers\V0\SLogger;
use App\Models\AbsModelFactory;
use App\Models\Orm\V0\UserCredit;
use App\Services\Core\Store\Hengxin\HengxinConfig;
use App\Services\Core\Store\HengxinSDK\AcpService;
use App\Services\Core\Store\HengxinSDK\CertUtil;
use App\Services\Core\Store\HengxinSDK\Common;
use App\Services\Core\Store\HengxinSDK\SDKConfig;


/**
 * 生成恒信订单号
 */
class BackTransFactory extends AbsModelFactory
{
    /**
     * 查询开通状态
     *
     * @param int $action_code
     * @return bool|string
     */
    public static function fetchBank($datas)
    {


        /***银联全渠道系统，产品参数，除了encoding自行选择外其他不需修改***/
        $bankData["version"] = HengxinConfig::VERSION;                  //版本号
        $bankData["encoding"] ="UTF-8";            //字符集编码 可以使用UTF-8,GBK两种方式
        $bankData["signMethod"] = HengxinConfig::signMethod;                           //签名方法 目前只支持01-RSA方式证书加密
        $bankData["txnType"] ="78";                              //交易类型 11-代收
        $bankData["txnSubType"] = "02";                           //交易子类型 00-默认开通
        $bankData["bizType"] = "000902";                          //业务类型 Token支付
        $bankData["channelType"] = "07";                          //渠道类型07-PC

        /***商户接入参数***/
        $bankData["merId"] = HengxinConfig::merId_no;                   			   //商户号码（本商户号码仅做为测试调通交易使用，该商户号配置了需要对敏感信息加密）测试时请改成自己申请的商户号，【自己注册的测试777开头的商户号不支持代收产品】
        $bankData["accessType"] = "0";                            //接入类型，商户接入固定填0，不需修改
        $bankData["orderId"] = $datas['orderId'];
//        $bankData["orderId"] = '201712271001492616403240121';
        //商户订单号，8-40位数字字母，不能含“-”或“_”，可以自行定制规则
        $bankData["txnTime"] = $datas['txnTime'];         				   //订单发送时间，格式为YYYYMMDDhhmmss，必须取当前时间，否则会报txnTime无效
//        $bankData["accessType"] = "01";
//                      //账号类型
        $bankData["encryptCertId"] = CertUtil::getEncryptCertId();
//        dd($bankData);
        AcpService::sign($bankData);

        $url = HengxinConfig::backTransUrl;
        $data = AcpService::post( $bankData, $url );

//        dd(urldecode($data['respMsg']));


        if (AcpService::validate($data)) {


            if ($data["respCode"] == "00") {
                //TODO


                $tokenPayData = urldecode($data["tokenPayData"]);
                $tokenPayData = Common::convertStringToArray(substr($tokenPayData, 1, strlen($tokenPayData) - 2));
                if (array_key_exists("token", $tokenPayData)) {

                    $token = $tokenPayData["token"];//tokenPayData其他子域均可参考此方式获取

                }


                $customerInfo = AcpService::parseCustomerInfo($data ["customerInfo"]);
                if (array_key_exists("phoneNo", $customerInfo)) {
                    $phoneNo = $customerInfo ["phoneNo"]; // customerInfo其他子域均可参考此方式获取

                }

//                dd($data);
                if ($data['activateStatus']) {
                    $orderId = $data['orderId'];
                    $tokenPayData['credit_card'] = AcpService::decryptData(urldecode($data['accNo']));
                    $tokenPayData['respCode'] = $data['respCode'];
                    $tokenPayData['respMsg'] = $data['respMsg'];
                    $user_id = BankFactory::addCredit($orderId, $tokenPayData);
                  //更新状态
                        $status['credit_card_status'] = 2;
                        UserStatusFactory::addUserStatus($user_id, $status);
                        return true;
                    SLogger::getStream()->info($phoneNo . '银行卡的token插入失败');

                } else {
                    $orderId = $data['orderId'];
                    $tokenPayData['credit_card'] = AcpService::decryptData($data['accNo']);
                    $user_id = BankFactory::addCredits($orderId, $tokenPayData);
                    return true;
                }


            } else {

                $info['is_query'] = 2;
                $info['respCode'] = $data['respCode'];
                $info['respMsg'] = urldecode($data['respMsg']);
                $user_id = UserCredit::where('orderId',$datas['orderId'])->update($info);
                return true;


            }
        }
    }
}